<?php
namespace Admin\Controller;
use Think\Controller;
class LookBookController extends Controller {
	//图书类型列表
    public function index(){
    	$id = I('get.id','');
		if(empty($id))
		{
			$this->error('没有找到此书籍');
		}else
		{
			$Model = new \Think\Model\MongoModel($id);
			$book_count = $Model->count();// 查询满足要求的总记录数
			include MODULE_PATH.'Common/'.'Page.class.php';
			$Page       = new \Page($book_count,500);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$show       = $Page->show();// 分页显示输出
			$this->assign('page',$show);// 赋值分页输出
			
			$list = $Model->order('_id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
			$this->assign('list',$list);
			$this->assign('id',$id);
			$book = D('Book')->find_book($id);
			$this->assign('book',$book[$id]);
			$this->assign('book_count',$book_count);
			$this->display();
		}
    }
	
	//添加图书类型
	public function add()
	{
		$submit = I('post.submit','');
		$id = I('get.id','');
		if(empty($id))
		{
			$this->error('没有找到此书籍');
			die();
		}
		
		if(!empty($submit))
		{	$title = I('post.title','');
			if(!empty($title) && !empty($id))
			{
				$keywords = I('post.keywords','');
				$description = I('post.description','');
				$status = I('post.status','1');
				$order = I('post.order','');
				$content = I('post.content','');
				$datetime = date('Y-m-d H:i:s',time());
				
				$data = array();
				$data['title'] = $title;
				$data['keywords'] = $keywords;
				$data['description'] = $description;
				$data['status'] = (int)$status;
				$data['order'] = (int)$order;
				$data['content'] = $content;
				$data['datetime'] = $datetime;
				$Model = new \Think\Model\MongoModel($id);
				$res = $Model->add($data);
				$article_id = (string)($res);
				if (!empty($article_id)) {
					$this->success('添加成功',__ROOT__.'/index.php/Admin/LookBook/index?id='.$id);
				}else {
					$this->error('添加失败');
				}
			}else{
				$this->error('标题不能为空');
			}
			
		}else {
			$this->assign('id',$id);
			$this->display();
		}
		
	}
	//修改图书类型
	public function edit()
	{
		$book_id = I('get.book_id','');
		$article_id = I('get.article_id','');
		if(empty($book_id) || empty($article_id))
		{
			$this->error('没有找到此文章');
		}else
		{
			$submit = I('post.submit','');
			$title = I('post.title','');
			$keywords = I('post.keywords','');
			$description = I('post.description','');
			$status = I('post.status','1');
			$order = I('post.order','');
			$content = I('post.content','');
			$datetime = date('Y-m-d H:i:s',time());
			$Model = new \Think\Model\MongoModel($book_id);
			
			if (!empty($submit)) {
				$data = array();
				$data['title'] = $title;
				$data['keywords'] = $keywords;
				$data['description'] = $description;
				$data['status'] = (int)$status;
				$data['order'] = (int)$order;
				$data['content'] = $content;
				$data['datetime'] = $datetime;
				$res =	$Model->where(array('_id'=>new \MongoId($article_id)))->save($data);
				if (!empty($res['ok'])) {
					$this->success('修改成功',__ROOT__.'/index.php/Admin/LookBook/index?id='.$book_id);
				}else {
					$this->error('修改失败');
				}
			}else {
				//查找书
				$article = $Model->where(array('_id'=>new \MongoId($article_id)))->select();
				$this->assign('article',$article[$article_id]);
				$this->assign('book_id',$book_id);
				$this->assign('article_id',$article_id);
				$this->display();
			}
		}
	}
	//删除图书
	public function del()
	{
		$book_id = I('get.book_id','');
		$article_id = I('get.article_id','');
		if(empty($book_id) || empty($article_id))
		{
			$this->error('没有找到此文章');
		}else
		{
			$Model = new \Think\Model\MongoModel($book_id);
			$res = $Model->delete($article_id);
			if ($res['ok']) {
				$this->success('删除成功');
			}else {
				$this->error('删除失败');
			}
		}
	}
	
}